package com.dycong.common.leetcode.yi;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-07-31
 * Time: 15:15
 */
public class ReverseList {


    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        listNode.next = new ListNode(2);
        listNode.next.next = new ListNode(3);
        listNode.next.next.next = new ListNode(4);
        listNode.next.next.next.next = new ListNode(5);

        ListNode result = reverseList(listNode);

//        while (result!=null){
//            System.out.print(result.val+"\t");
//            result=result.next;
//        }

        System.out.println(majorityElement(new int[]{2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1}));
    }

    public static ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode pNext = head.next; //保存当前结点的下一结点
        ListNode result = reverseList(pNext);
        pNext.next = head;

        head.next = null;
        return result;
    }


    public static int majorityElement(int[] nums) {
        int num = nums[0];
        int count = 0;

        for (int i = 0; i < nums.length; i++) {
            if (num == nums[i]) {
                count++;
            } else {
                count--;
                if (count == 0) {
                    num = nums[i];
                    count++;
                }
            }
        }

        return num;
    }
}
